package main

import (
	"fmt"
	"sync"
)

func main() {
	c := MyMap{}
	c.Map = make(map[int]int)
	go func() { //开一个协程写map
		c.L.Lock()
		defer c.L.Unlock()
		for j := 0; j < 10000; j++ {
			c.Map[j] = j
		}
	}()

	for j := 0; j < 10000; j++ {
		go func(k int) { //开一个协程读map
			c.L.RLock()
			defer c.L.RUnlock()
			fmt.Println(c.Map[k])
		}(j)
	}

}

type MyMap struct {
	Map map[int]int
	L   sync.RWMutex
}
